package com.wedo.mall.adapter;

import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.adapter.Adapter;
import com.wedo.mall.R;
import com.wedo.mall.entity.Goods;
import com.wedo.mall.entity.Shopping;
import com.wedo.mall.util.WUrl;
import com.wedo.mall.widget.ShoppingView;

import java.util.List;

/**
 * Created by e on 2017/7/27.
 */

public class ShoppingAdapter extends Adapter<Shopping> {

    /**编辑购物车*/
    private boolean editable = false;
    /**是否可选：确认订单页面为false*/
    private boolean checkable = true;
    private boolean onBinding = false;
    private boolean swipe = true;

    /**送货方式*/
    private String express_type;

    /**选择商品属性*/
    private ShoppingView.OnAttrClickListener mOnAttrClickListener;
    public ShoppingAdapter(Context context, List<Shopping> datas) {
        super(context, datas);
    }

    @Override
    protected int layout() {
        return R.layout.item_shopping;
    }

    @Override
    protected void bind(EHolder holder, final Shopping shopping, final int position) {
        CheckBox cb_agent = holder.find(R.id.cb_agent);
        ImageView iv_agent = holder.find(R.id.iv_agent);
        TextView tv_agent = holder.find(R.id.tv_agent);
        final ShoppingView sv_goods = holder.find(R.id.sv_goods);

        onBinding = true;
        cb_agent.setChecked(shopping.check);
        onBinding = false;

        tv_agent.setText(shopping.agent_name);
        sv_goods.shopping(shopping).adapter(this).observable(onObservableCheck)
                .agent(cb_agent).editable(editable).checkable(checkable).swipe(swipe)
                .setAttrListener(mOnAttrClickListener).setOnDeleteGoods(onDeleteGoods).express(express_type).goodsList(shopping.goodsList);
        Glide.with(mContext).load(WUrl.url(shopping.logo))
                .error(R.drawable.place_holder_agent).placeholder(R.drawable.place_holder_agent)
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_agent);

        cb_agent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(!onBinding) {
                    sv_goods.check(isChecked);
                    shopping.check = isChecked;

                    if(onObservableCheck !=null) {
                        onObservableCheck.onChecked(isCheckAll());
                    }
                }

            }
        });
    }

    /**编辑购物车*/
    public void editable(boolean editable){
        this.editable = editable;
        notifyDataSetChanged();
    }

    /**可选模式：确认订单页面设置为false*/
    public void checkable(boolean checkable){
        this.checkable = checkable;
    }

    /**是否可侧滑*/
    public void swipe(boolean swipe){
        this.swipe = swipe;
        notifyDataSetChanged();
    }

    public void setAttrListener(ShoppingView.OnAttrClickListener onAttrClickListener) {
        this.mOnAttrClickListener = onAttrClickListener;
    }

    /**全选或反选*/
    public void check(boolean check){
        if(isEmpty()) {
            return;
        }
        for (Shopping shopping:mDatas){
            shopping.check = check;
            List<Goods> goods = shopping.goodsList;
            for (Goods g:goods){
                g.check = check;
            }
        }
        notifyDataSetChanged();
    }

    public boolean isCheckAll(){
        if(isEmpty()) {
            return false;
        }
        boolean isCheckAll = true;
        for (Shopping shopping:mDatas){
            if(!shopping.check) {
                isCheckAll = false;
                return isCheckAll;
            }else {
                List<Goods> goodsList = shopping.goodsList;
                for (Goods goods:goodsList){
                    if(!goods.check) {
                        isCheckAll = false;
                        return isCheckAll;

                    }
                }
            }
        }
        return isCheckAll;
    }

    public void setExpress_type(String express_type) {
        this.express_type = express_type;
        notifyDataSetChanged();
    }

    public void setOnObservableCheck(OnObservableCheck onCheckChange) {
        this.onObservableCheck = onCheckChange;
    }
    private OnObservableCheck onObservableCheck;
    public interface OnObservableCheck {
        void onChecked(boolean check);
    }

    public void setOnDeleteGoods(OnDeleteGoods onDeleteGoods) {
        this.onDeleteGoods = onDeleteGoods;
    }

    private OnDeleteGoods onDeleteGoods;
    public interface OnDeleteGoods{
        void delete(Goods goods);
    }
}
